angular.module('starter.controllers', [])
//首页面
.controller('MainCtrl', function($scope,$state,$ionicPopup) {
    alertPopupFun($ionicPopup,'loading','正在加载中...');
    $scope.selectedIndex = 1;
    //点击最左侧
    $scope.clickMain = function (i){
        $scope.selectedIndex = i;
        $state.go('menu.list');
    }
    //退出应用
    $scope.gohomeFun = function (){
        closeWebView("",function (){
            console.log('应用退出成功！');
        },function (){
            console.log('应用退出失败！');
        });
    }
})
//菜单
.controller('MenuCtrl', function($scope,$state,$ionicPopup,$timeout,agentList) {
    $scope.agentList = agentList;
    alertPopup.close();//记得删掉哦
    // $timeout(function (){
    //     loadAgent($scope);//查询当前登录人及其直属下属人员列表  
    // },1000) 
    //点击菜单
    $scope.clickMenu = function (item){
        // alertPopupFun($ionicPopup,'loading','正在加载中...');
        $scope.selectedRow = item.agentCode;
    	if('pad' == platform){
			$state.go('menu.list.data_view',{'code':item.agentCode});
    	}else{
			$state.go('data_view',{'code':item.agentCode});
    	}	
    }
})
//数据展示
.controller('DataCtrl', function($scope,$state,$stateParams,$compile,$rootScope,$ionicModal,$ionicPopup,$timeout) {
	
    var currentDate = new Date();
    var dateStr =currentDate.getFullYear()+"年"+(currentDate.getMonth()+1)+"月";
    var date = currentDate.getFullYear()+"-"+(currentDate.getMonth()+1);
    $scope.selectDate = dateStr;
    if($stateParams.code){
		$scope.title = "收费列表";
        $timeout(function (){
            loadPolicyByAgent($stateParams.code,date,$scope);//查询当前业务员保单信息
        },1000)
	}
     //关闭事件
    $scope.closeDateView = function (){
        $state.go('menu.list');
    }
    $ionicModal.fromTemplateUrl('templates/fee_detials.html', {
    scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    //关闭按钮
    $scope.closePopUp = function() {
      $scope.modal.hide();
    };
    //查看详情
    $scope.detialsPopUp = function(item) {
      $scope.detials = item;
      $scope.modal.show();
    };
    // 日期选择
    $scope.chooseDate = function(){
        // calenderFun();
        datePicker('',function (date){

            $scope.$apply(function (){
                $scope.selectDate = date.year+"年"+date.month+"月";
                loadPolicyByAgent($stateParams.code,date.year+"-"+date.month,$scope);//查询当前业务员保单信息
            }); 
        },function (){
            console.log("日期插件调用失败！")
       });
    }
    //收费
    $scope.applyPolicy = function(item) {
        if('0000' == item.payState){
            alertPopupFun($ionicPopup,'loading','正在收费中...');
        } 
        applyPolicy(item,$scope,$timeout,$ionicPopup) 
    }  
})